package com.common.base.tools.extend

import android.app.Activity
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.XXPermissions

/**
 * Created by zsf on 2020/11/19 14:51
 * ******************************************
 * * 权限统一处理，复制使用方法到Activity'
 * ******************************************
 */
/**
 * 使用方式
requestPermission(
mutableListOf(
Manifest.permission.READ_CONTACTS,
Manifest.permission.CAMERA,
Manifest.permission.CALL_PHONE
), {

}, {
Toast.makeText(this, "不同意的: $it", Toast.LENGTH_LONG).show()
})
 */
fun Activity.requestPermission(permissions:List<String>, onSuccess: () -> Unit, onFailed: (MutableList<String>,Boolean) -> Unit) {

    XXPermissions.with(this)
        // 申请单个权限
//            .permission(Permission.RECORD_AUDIO)
        // 申请多个权限
//            .permission(Permission.Group.CALENDAR)
        .permission(permissions)
        // 设置权限请求拦截器（局部设置）
        //.interceptor(new PermissionInterceptor())
        // 设置不触发错误检测机制（局部设置）
        //.unchecked()
        .request(object : OnPermissionCallback {

            override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
//                if (!allGranted) {
//                    Toast.makeText(activity,"部分权限未正常授予", Toast.LENGTH_LONG).show()
////                        toast("获取部分权限成功，但部分权限未正常授予")
//                    return
//                }
                onSuccess()
            }

            override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                onDenied(permissions, doNotAskAgain)
                onFailed(permissions,doNotAskAgain)
//                if (doNotAskAgain) {
//                    Toast.makeText(activity,"永久拒绝:", Toast.LENGTH_LONG).show()
//                    RouterLogger.toast("被永久拒绝授权，请手动授予录音和日历权限")
//                    如果是被永久拒绝就跳转到应用权限系统设置页面
//                    XXPermissions.startPermissionActivity(activity, permissions)
//                } else {
//                    RouterLogger.toast("获取录音和日历权限失败")
//                }
            }
        })
}